package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.http.HttpMethods;
import com.tepth.maintenancesystem.bean.UserEntity;

import rx.Subscriber;

public class HttpTestActivity extends AppCompatActivity implements View.OnClickListener {

    String vehicleOnlineResult;
    TextView resultTV;
    private Context context;
    private Subscriber login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_test);
        Button clickMeBN = (Button) findViewById(R.id.btn_test);
        resultTV = (TextView) findViewById(R.id.result_TV);
        clickMeBN.setOnClickListener(this);
        context=this;
    }

    //进行网络请求
    private void getMovie() {
//        String baseUrl = "http://dec2.tepth.cn/";
//
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(baseUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .build();
//
//        LoginService loginService = retrofit.create(LoginService.class);
//        //platform=SmartFam&action=Login&loginName=ltsby4z&password=ltsby4z&appName=smartFAM_apk&appVersion=1.0&osName=Android&osVersion=6.0
//        loginService.getLoginObj("SmartFam", "Login", "ltsby4z", "ltsby4z", "smartFAM_apk", "1.0", "Android", "6.0")
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<LoginObj>() {
//                    @Override
//                    public void onCompleted() {
//                        Toast.makeText(context, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        resultTV.setText(e.getMessage());
//                    }
//
//                    @Override
//                    public void onNext(LoginObj loginObj) {
//                        resultTV.setText(loginObj.toString());
//                    }
//                });
        login=new Subscriber<UserEntity>() {
            @Override
            public void onCompleted() {
                Toast.makeText(context, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable e) {
                resultTV.setText(e.getMessage());
            }

            @Override
            public void onNext(UserEntity userEntity) {
                resultTV.setText(userEntity.toString());
            }
        };
        HttpMethods.getInstance().login(login, 0, 10);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    resultTV.setText(vehicleOnlineResult);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        getMovie();
    }
}
